package com.app.task;

import java.util.ArrayList;


public abstract class Task implements Runnable{
	private boolean stop;
	private ArrayList<TaskWatcher> watchers = new ArrayList<TaskWatcher>();
	public void stop(){
		stop = true;
	}
	@Override
	public void run() {
		stop = false;
                beforeTask();
		while(!stop){
			task();
		}
	}
	protected abstract void task();
	
	public void addTaskWatcher(TaskWatcher watcher){
		watchers.add(watcher);
	}
	public void removeTaskWatcher(TaskWatcher watcher){
		watchers.remove(watcher);
	}
	public void taskComplete(){
		stop();
		for(TaskWatcher watcher: watchers){
			watcher.taskComplete(this);
		}
	}
        public void beforeTask(){
           
        }
}
